package com.cloxtec.scada.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component
public class ParamHelper {

    private static final Logger log = LoggerFactory.getLogger(ParamHelper.class);
    @Resource
    private MongoHelper mongoHelper;

    private boolean logMsg;

    ParamHelper() {
        logMsg = false;
    }

    public void setLogMsg(boolean flag) {
        logMsg = flag;
    }


    public boolean isLogMsg() {
        return logMsg;
    }


    public List<JSONObject> getParam(String collection) {
        List<JSONObject> lstObj = mongoHelper.find(collection);
        if (lstObj == null){
            lstObj = new ArrayList<>();
        }
        return lstObj;
    }

    public JSONArray getParamArray(String collection) {
        List<JSONObject> lstObj = getParam(collection);
        int nSize = lstObj.size();
        JSONArray array = new JSONArray();
        for (int i = 0; i < nSize; i++) {
            array.add(lstObj.get(i));
        }
        return array;
    }

    public JSONObject getParamById(String collection, String id) {
        Map<String, Object> criteria_params = new HashMap<String, Object>();
        criteria_params.put("_id", id);

        List<JSONObject> lstObj = mongoHelper.find(collection, criteria_params);
        if (lstObj != null) {
            if (lstObj.size() > 0) {
                return lstObj.get(0);
            }
        }

        return null;
    }

    public JSONArray getParamByKeyValue(String collection, String key, String value) {
        Map<String, Object> criteria_params = new HashMap<String, Object>();
        criteria_params.put(key, value);

        List<JSONObject> lstObj = mongoHelper.find(collection, criteria_params);
        int nSize = lstObj.size();
        JSONArray array = new JSONArray();
        for (int i = 0; i < nSize; i++) {
            array.add(lstObj.get(i));
        }
        return array;
    }


    public boolean updateParam(String collection, JSONObject param) {
        Map<String, Object> criteria_params = new HashMap<String, Object>();
        criteria_params.put("_id", param.getString("_id"));
        return mongoHelper.update(collection, criteria_params, param);
    }

    public boolean updateParam(String collection, String id, String key, String value) {
        Map<String, Object> criteria_params = new HashMap<String, Object>();
        criteria_params.put("_id", id);
        return mongoHelper.update(collection, criteria_params, key, value);
    }

    public boolean updateParam(String collection, String id, String key, boolean value) {
        Map<String, Object> criteria_params = new HashMap<String, Object>();
        criteria_params.put("_id", id);
        return mongoHelper.update(collection, criteria_params, key, value);
    }

    public boolean updateParam(String collection, String id, String key, JSONObject value) {
        Map<String, Object> criteria_params = new HashMap<String, Object>();
        criteria_params.put("_id", id);
        return mongoHelper.update(collection, criteria_params, key, value);
    }


    public boolean updateParam(String collection, String id, String key, JSONArray value) {
        Map<String, Object> criteria_params = new HashMap<String, Object>();
        criteria_params.put("_id", id);
        return mongoHelper.update(collection, criteria_params, key, value);
    }

    public boolean deleteParam(String collection, String id) {
        Map<String, Object> criteria_params = new HashMap<String, Object>();
        criteria_params.put("_id", id);

        long nRet = mongoHelper.delete(criteria_params, collection);
        return nRet > 0 ? true : false;
    }

    public boolean deleteParamByKeyValue(String collection, String key, String value) {
        Map<String, Object> criteria_params = new HashMap<String, Object>();
        criteria_params.put(key, value);

        long nRet = mongoHelper.delete(criteria_params, collection);
        return nRet > 0 ? true : false;
    }

    public boolean saveParam(String collection, JSONObject jsonParam) {
        return mongoHelper.save(collection, jsonParam);
    }

    public JSONObject deepMerge(JSONObject source, JSONObject target) throws JSONException {
        Set<String> keys = source.keySet();
        if (keys.isEmpty()) {
            return target;
        }
        for (String key : keys) {
            Object value = source.get(key);
            if (!target.containsKey(key)) {
                // new value for "key":
                target.put(key, value);
            } else {
                // existing value for "key" - recursively deep merge:
                if (value instanceof JSONObject) {
                    JSONObject valueJson = (JSONObject) value;
                    deepMerge(valueJson, target.getJSONObject(key));
                } else {
                    target.put(key, value);
                }
            }
        }
        return target;
    }
}
